from flask import request
from config import app,redis_service
from services import user_service, procure_order_service, trans_order_service,home_service



from results import *


@app.before_request
def before():
    url = request.path
    if url == '/login' or url == '/register':
        pass
    else:
        token = request.headers.get('token')
        if token is None:
            return not_login_result
        username = redis_service.get(token)
        if username:
            pass
        else:
            return not_login_result


@app.post('/login')
def login():
    get_json = request.json
    return user_service.login_impl(get_json)


@app.post('/register')
def register():
    """_summary_
    注册接口
    传入参数:
    @
    Returns:
        _type_: _description_
    """
    get_json = request.json
    return user_service.register_impl(get_json)

@app.post('/user_info_query')
def user_info_query():
    """
    获取用户信息查询接口
    @输入参数:
    address:
    @输出参数:
    ..
    """
    get_json = request.json
    return user_service.query_user_info(get_json['address'])

@app.post("/procure_order_create")
def procure_order_create():
    get_json = request.json
    return procure_order_service.create_procure_order(get_json)

@app.post('/procure_order_query')
def procure_order_query():
    """通过采购公司地址获取采购订单

    Returns:
        _type_: _description_
    """
    get_json = request.json
    return procure_order_service.query_procure_order(get_json)

@app.post("/procure_order_query_by_id")
def procure_order_query_by_id():
    get_json = request.json
    return procure_order_service.query_procure_order_by_id(get_json)

@app.post('/trans_order_create')
def trans_order_create():
    get_json = request.json
    return trans_order_service.create_trans_order(get_json)

@app.post('/trans_order_query_by_company')
def trans_order_query_by_company():
    get_json = request.json
    return trans_order_service.query_procure_order_by_company(get_json)

@app.get('/home_page_query')
def home_page_query():
    return home_service.home_page_query()

if __name__ == '__main__':
    app.run()
